package com.imooc.distributedemo.leetcode;

/**
 * 二分查找
 */
public class MidarySearch {
    public static void main(String[] args) {
        int[] array = {-4,0,1,12,34,223,1325,1622,2333,3522,8338,8888};
        int target = 1;
        int index = search(array,target);
        System.out.println(index);
    }

    private static int search(int[] array,int target) {
        // 初始设定i/j为左右
        int i =0;
        int j = array.length-1;
        while (i<=j){
            int m = (i+j)/2;
            if(array[m]<target){
                i = m+1;
            }else if(array[m]>target){
                j = m-1;
            }else{
                return m;
            }
        }
        return -1;
    }
}
